<?php

/*
 * 慈铭体检报告解析
 */
// cimingParase("./%2F000101014061871575.html");
cimingParase("./bb.html");
// cimingParase("./aa.html");
function cimingParase($filename) {
	$content = file_get_contents($filename);

	//$content = mb_convert_encoding($content, 'UTF-8', 'GB2312,GBK,UTF-8');
	$encode = mb_detect_encoding($content, array("ASCII", 'UTF-8', 'GB2312', "GBK"));
	if ($encode == "EUC-CN") {
		$content = iconv("GB2312", "UTF-8", $content);
	}
	if($encode == "CP936"){
		$content = mb_convert_encoding($content, 'UTF-8', 'GB2312,GBK,UTF-8');
	}
	$data = array();  //所有内容
	//个人基本信息
	$patten_basic = "/(以下内容由客人本人提供)(.*)<div class=div647>/isU";

	preg_match($patten_basic, $content, $matches);
	//	var_dump($matches);exit();
	$basic_info = $matches[0];
	$pattern = "/<td(.*)<\/td>/isU";
	preg_match_all($pattern, $basic_info, $matches);

	foreach ($matches[0] as $key => $val) {
		if ($key % 2 != 0) {
			$basic_info_values[] = trim(strip_tags($val));
		} else {
			$basic_info_keys[] = trim(strip_tags($val));
		}
	}

	$basic_itemSet = array();
	$basic_info = array_combine($basic_info_keys, $basic_info_values);

	//获取慈铭体检报告个人信息，便于入库，插入“牛逼体检报告”项目中的report.reportinfo表
//		$reportinfo=array();
	$reportinfo = array('realname' => '', 'gender' => '', 'examno' => '', 'examdate' => '');
	$reportinfo['realname'] = $basic_info['姓名'];
	$reportinfo['gender'] = $basic_info['性别'];
	if (array_key_exists("体检时间", $basic_info)) {
		$reportinfo['examdate'] = $basic_info['体检时间'];
	}


//		var_dump($reportinfo);exit();
	foreach ($basic_info as $key => $val) {
		//我想在这里删掉内容为空的小项，但是删不掉？？？？？？？？？？
		if (empty($val)) {
			unset($basic_info[$key]);
		}

		array_push($basic_itemSet, array("itemName" => $key, "itemValue" => $val));
	}

//		var_dump($basic_itemSet);exit();
//		var_dump($basic_info);exit();
	$basic_info = array("parentName" => "basic", "itemSet" => $basic_itemSet);

	array_push($data, $basic_info);



	//匹配大项，筛选出每一大项的内容
	$pattern = "/<strong>&nbsp;&nbsp;<u>(.*)<\/div>/isU";
	preg_match_all($pattern, $content, $matches);


	//循环每一大项
	foreach ($matches[0] as $key => $val) {
		//var_dump($val);continue;
		//匹配体检项目大项名称
		$patten_parentName = "/<strong>&nbsp;&nbsp;<u>(.*)<\/u><\/strong>/isU";
		preg_match_all($patten_parentName, $val, $matches_parentName);
		$parentName = trim($matches_parentName[1][0]);

		//筛选出每一小项的所有html内容
		$pattern_tr = "/<td valign=top>(.*)<\/tr>/isU";
		//$patten_tr="/<tr(.*)<\/tr>/isU";
		preg_match_all($pattern_tr, $val, $matches);
		//var_dump($matches[0]);continue;
		//array_shift($matches[0]);
		//var_dump($matches[0]);continue;
		//定义小项key的名称
		
		$keys_two = array("itemName", "itemValue");
		$keys_three = array("itemName", "itemValue", "unit");
		$keys_four = array("itemName", "itemValue", "unit", "normal");
		$keys_five = array("itemName","itemValue","issue","unit","normal");

		//获取每一条小项的指标数值
		$itemSet = array();
		foreach ($matches[0] as $key => $val) {
			//			var_dump($val);continue;
			//提取“项目”，“单位”，“结果”，“参考范围”的值
			$patten = "/<td(.*)<\/td>/isU";
			//var_dump($val);continue;
			preg_match_all($patten, $val, $matches_item);
			// var_dump($matches_item);continue;
			$array_item = array();
			foreach ($matches_item as $key => $val) {
				$array_inner = array();
				foreach ($val as $k => $v) {
					//去掉空格
					$str = trim(strip_tags($v));
					$new_str = str_replace("&nbsp;", '', $str);
					array_push($array_inner, $new_str);
				}
				//				var_dump($val);continue;
				array_push($array_item, $array_inner);
			}
			// unset($array_item[0][2]);
			//var_dump($array_item[0]);continue;
			//把每一小项目的key值和value值对应上
			if (count($array_item[0]) === 4) {
				$rets = array_combine($keys_four, $array_item[0]);
			} elseif (count($array_item[0]) === 2) {
				$rets = array_combine($keys_two, $array_item[0]);
			} elseif (count($array_item[0]) === 3) {
				$rets = array_combine($keys_three, $array_item[0]);
			}elseif(count($array_item[0]) === 5){
				$rets = array_combine($keys_five, $array_item[0]);
			}

			array_push($itemSet, $rets);
		}
		//		exit();
		//各大项名称和大项指标合并
		$item = array("parentName" => $parentName, "itemSet" => $itemSet);
		array_push($data, $item);
	}

	//匹配“总汇”内容
	$patten_confluence = "/<center>(.*)<\/div>/isU";
	preg_match($patten_confluence, $content, $matches);

	$confluence = array("parentName" => "总汇",
		"itemSet" => array(array("itemName" => "总汇", "itemValue" => trim(strip_tags($matches[1]))))
	);
	array_push($data, $confluence);
//			var_dump($data);exit();
	//return json_encode(array("data"=>$data,"reportinfo"=>$reportinfo),JSON_UNESCAPED_UNICODE);
	return json_encode(array("data" => $data, "reportinfo" => $reportinfo),JSON_UNESCAPED_UNICODE);
//		$json = json_encode($data,JSON_UNESCAPED_UNICODE);
//	 	$json = str_replace('\r\n\r\n', '', $json);
//	 	$json = str_replace('\r', '', $json);
//	 	$json = str_replace('\n', '', $json);
	//echo $json;

	/*
     * 体检报告格式统一化
     */
//	 	$arrayItemResult=json_decode($json,true);
//	 	
//	 	$p=new reportStandard();
//		$p->standard($arrayItemResult, "ciming");
}
